package cn.qrx.gateway.seesion.defaults;

import cn.qrx.gateway.bind.IGenericReference;
import cn.qrx.gateway.executor.Executor;
import cn.qrx.gateway.mapping.HttpStatement;
import cn.qrx.gateway.seesion.Configuration;
import cn.qrx.gateway.seesion.GatewaySession;

import java.util.Map;

/**
 * @Author: 屈燃希
 * @Date:   2025/10/15
 * @Version: V1.0
 * @Description:
 */
public class DefaultGatewaySession implements GatewaySession {

    private Configuration configuration;

    private String uri;


    private Executor executor;


    public DefaultGatewaySession(Configuration configuration, String uri, Executor executor) {
        this.configuration = configuration;
        this.uri = uri;
        this.executor = executor;
    }

    @Override
    public Object get(String methodName, Map<String,Object> params) {

        HttpStatement httpStatement = configuration.getHttpStatement(uri);
        try {
            return executor.execute(httpStatement, params);
        } catch (Exception e) {
            throw new RuntimeException("Error exec get. Cause: " + e);
        }


    }

    @Override
    public Object post(String methodName, Map<String, Object> params) {
        return get(methodName, params);
    }

    @Override
    public IGenericReference getMapper(String uri) {
        return configuration.getMapper(uri, this);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }

}
